package ru.st.selenium.pages;

import com.opera.core.systems.internal.CallbackWait;
import org.apache.commons.codec.language.Nysiis;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.*;
import ru.st.selenium.util.MyUtil;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: gor
 * Date: 20.10.13
 * Time: 19:57
 * To change this template use File | Settings | File Templates.
 */
public class HomeMainPage {

    public static void addMovie(WebDriver driver){
        driver.findElement(By.xpath("//img[@title='Add movie']")).click();
    }

    public static void clickMyCollectionAkaHome(WebDriver driver){

    }

    public static String[] getLinksToFilms(WebDriver driver){

        if (!MyUtil.isElementPresent(driver, By.xpath("//div[@id='results']"))){
            System.out.println("NO Results at all?");
        }

        List<WebElement> webElements = driver.findElements(By.xpath("//div[@id='results']/a[@href]"));
        //TODO - а если null?
        String[] result = new String[webElements.size()];
        for (int i = 0; i < webElements.size(); i++) {
            result[i] = webElements.get(i).getAttribute("href");
//            System.out.println(webElements.get(i).getAttribute("href"));
        }

        return result;
    }

    public static String[] getTitles(WebDriver driver) throws InterruptedException {
        Thread.sleep(2000);  //DEBUG странно...
        if (!MyUtil.isElementPresent(driver, By.xpath("//div[@id='results']"))){
            System.out.println("NO Results at all?");
        }

        List<WebElement> webElements = driver.findElements(By.xpath("//div[@id='results']/a/div/div[2]"));

        String[] result = new String[webElements.size()];

        for (int i = 0; i < webElements.size(); i++) {
            result[i] = webElements.get(i).getText();

        }

        return result;
    }

    public static boolean doSearch(WebDriver driver, String searchString) throws InterruptedException {
        driver.findElement(By.id("q")).clear();
        driver.findElement(By.id("q")).sendKeys(searchString);
        driver.findElement(By.id("q")).sendKeys(Keys.RETURN);
//        Thread.sleep(3000);//поспим немного, ибо не понятно как выстявлять флаг, что поиск окончен
        String[] res = getTitles(driver);
        String[] results;
        int counter = 0;
        while(true){
            results = getTitles(driver);
            if (counter >= 10){
                System.out.println("Долго ждем");
                break;
            }
            if (res.length == results.length){
                break;
            }
        }

        System.out.println("Поиск произведен!");
        return true;
    }

    public static boolean isMessageNothingFoundDisplayed(WebDriver driver){
        if (MyUtil.isElementPresent(driver, By.xpath("//div[@class='content']"))){
            String message = driver.findElement(By.xpath("//div[@class='content']")).getText();
//            System.out.println("DEBUG isMessageNothingFoundDisplayed text = " + message);
            if (message.contains("No movies where found.")){
                return true;
            }
            else {
                return false;
            }
        }
        else {
            return false;
        }
    }

}
